//
//  TopTabBaseController.m
//  NJUSTHelper
//
//  Created by 郑越辉 on 2017/9/21.
//  Copyright © 2017年 njust. All rights reserved.
//

#import "TopTabBaseController.h"
#import "EveryDayVC.h"
#define WEEK_DAY_COUNT 7
#define TitleHeaght 35
///滑动到下一周的力度, 0~1之间
#define Slide_Size 0.88
@interface TopTabBaseController () <UIScrollViewDelegate>{
    ///判断是否初始化过
    BOOL _isInitedScrollView;
}

///承载课表的ScrollView
@property (nonatomic, strong) UIScrollView *mainScrollView;
///承载日期的ScrollView
@property (nonatomic, strong) UIScrollView *headerScrollView;
///显示被选中的日期的ScrollView
@property (nonatomic, strong) UIScrollView *headerSelectedScrollView;

@property (nonatomic, strong) UIView *headerSelectedAreaShowView;
///保存头部按钮
@property (nonatomic, strong) NSMutableArray *headBtnArray;
///头部选中的cell数组
@property (nonatomic, strong) NSMutableArray *headCellViewSelectedArray;
///头部cell数组
@property (nonatomic, strong) NSMutableArray * headCellViewArray;
///7个课表VC数组
@property (nonatomic, strong) NSMutableArray *everyDayVCArray;
@end

@implementation TopTabBaseController
#pragma mark 懒加载
-(NSMutableArray *)everyDayVCArray{
    if(!_everyDayVCArray){
        _everyDayVCArray = [NSMutableArray array];
    }
    return _everyDayVCArray;
}
-(NSMutableArray *)headCellViewArray{
    if(!_headCellViewArray){
        _headCellViewArray = [NSMutableArray array];
    }
    return _headCellViewArray;
}
-(NSMutableArray *)headCellViewSelectedArray {
    if(!_headCellViewSelectedArray) {
        _headCellViewSelectedArray = [NSMutableArray array];
    }
    return _headCellViewSelectedArray;
}
-(NSMutableArray *)headBtnArray {
    if(!_headBtnArray){
        _headBtnArray = [NSMutableArray array];
    }
    return _headBtnArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.backgroundColor = [UIColor whiteColor];
    ///默认显示当天的课表
    self.firstDay = [NSDate getFirstDay];
    self.currentDay = [NSDate getTodayNoonDate];
    ///加1, 显示周次
    self.currentWeekNum = [self.firstDay compsWeekToDate:self.currentDay] + 1;
    self.currentWeekDay = [self.currentDay getWeekIndex];
    self.startMonday = [self.currentDay getMondayThisWeek];
    ///设置样式
    self.titleBakcgroundColor = [UIColor lightGrayColor];       //标签未选择的背景颜色
    self.titleColor = [UIColor whiteColor];                     //标签未选择颜色
    self.titleSelectedBakcgroundColor = [UIColor whiteColor];   //标签选中背景颜色
    self.titleSelectedColor = Main_Navigation_Color;            //标签选中文字颜色
    self.bottomLineColor = Main_Navigation_Color;               //标签选中底边颜色
    
    _isInitedScrollView = NO;
    
}
///显示课表界面
- (void)showSchedule
{
    ///若为初始化,先进行初始化
    if(!_isInitedScrollView){
        ///设置尺寸
        self.buttonHeight = TitleHeaght;
        self.buttonWidth = self.width/WEEK_DAY_COUNT;
        [self initBackView];
        [self initFixedHeader];
        [self initEveryDayVCArray];
        for (NSInteger index = 0; index < WEEK_DAY_COUNT; index++) {
            [self addEveryDayVCWithIndex:index];
        }
    }
    ///显示当天课表
    [self showScheduleWithIndex:[self.currentDay getWeekIndex]];
    
    
    ///设置为已经初始化
    _isInitedScrollView = YES;
}

///显示星期(index+1)的课表
-(void)showScheduleWithIndex:(NSInteger)index{
    [self.mainScrollView scrollRectToVisible:CGRectMake(index*self.width, 0, self.mainScrollView.frame.size.width, self.mainScrollView.frame.size.height) animated:NO];
}


////初始化7个VC
-(void)initEveryDayVCArray{
    for (int i = 0; i < WEEK_DAY_COUNT; i++) {
        EveryDayVC * viewController = [[EveryDayVC alloc] init];
        [viewController.view setFrame:CGRectMake(self.width*i, 0, self.width, self.height - self.buttonHeight - self.bottomHeight)];
        viewController.showDate = [self.startMonday dayAfterDays:i];
        [self.everyDayVCArray addObject:viewController];
    }
}
////日期更新,7个VC的课表/头部日期更新 data为周一日期
-(void)setDateForScheduleWithThisWeekMonday:(NSDate *)monday{
    for (EveryDayVC *vc in self.everyDayVCArray) {
        vc.showDate = [monday dayAfterDays:[self.everyDayVCArray indexOfObject:vc]];
    }
    for (TTHeadTitleView *view in self.headCellViewArray) {
        view.date = [monday dayAfterDays:[self.headCellViewArray indexOfObject:view]];
    }
    for (TTHeadTitleView *view in self.headCellViewSelectedArray) {
        view.date = [monday dayAfterDays:[self.headCellViewSelectedArray indexOfObject:view]];
    }
}


////添加第n个VC
- (void)addEveryDayVCWithIndex:(NSInteger)index
{
    EveryDayVC *vc = [self.everyDayVCArray objectAtIndex:index];
    [self addChildViewController:vc];
    [self.mainScrollView addSubview:vc.view];
}
///初始化主体背景
- (void)initBackView
{
    self.mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.buttonHeight, self.width, self.height - self.buttonHeight)];
    self.mainScrollView.contentSize = CGSizeMake(self.width*(WEEK_DAY_COUNT), self.height - self.buttonHeight);
    [self.mainScrollView setPagingEnabled:YES];
    [self.mainScrollView setShowsVerticalScrollIndicator:NO];
    [self.mainScrollView setShowsHorizontalScrollIndicator:NO];
    self.mainScrollView.backgroundColor = self.backgroundColor;
    self.mainScrollView.bounces = YES;
    self.mainScrollView.delegate = self;
    [self.view addSubview:self.mainScrollView];
}
///初始化头部日期标签
- (void)initFixedHeader
{
    ///可见区域大小
    self.headerScrollView.frame = CGRectMake(0, 0, self.width, self.buttonHeight);
    ///内容区域大小
    self.headerScrollView.contentSize = CGSizeMake(self.width, self.buttonHeight);
    [self.view addSubview:self.headerScrollView];
    ///
    for (NSInteger index = 0; index < WEEK_DAY_COUNT; index++) {
        
        TTHeadTitleView *headCellView = [[TTHeadTitleView alloc]initWithFrame:CGRectMake(self.buttonWidth*index, 0, self.buttonWidth, self.buttonHeight)];
        headCellView.fontColor = self.titleColor;
        headCellView.date = [self.startMonday dayAfterDays:index];
        [self.headerScrollView addSubview:headCellView];
        headCellView.backgroundColor = self.titleBakcgroundColor;
        [self.headCellViewArray addObject:headCellView];
        
        UIButton *headbtn = [[UIButton alloc] initWithFrame:CGRectMake(self.buttonWidth*index, 0, self.buttonWidth, self.buttonHeight)];
        headbtn.tag = index;
        [headbtn setBackgroundColor:[UIColor clearColor]];
        [headbtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.headerScrollView addSubview:headbtn];
        [self.headBtnArray addObject:headbtn];
        
    }
    self.headerSelectedAreaShowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.buttonWidth, self.buttonHeight)];
    [self.headerScrollView addSubview:self.headerSelectedAreaShowView];
    ///当headerSelectedAreaShowView移动时,带着headerSelectedScrollView的frame移动,移动到的区域可显示contentSize的内容
    self.headerSelectedScrollView.frame =CGRectMake(0, 0, self.buttonWidth, self.buttonHeight);
    self.headerSelectedScrollView.contentSize = CGSizeMake(self.width, self.buttonHeight);
    [self.headerSelectedAreaShowView addSubview:self.headerSelectedScrollView];
    
    for (NSInteger index = 0; index < WEEK_DAY_COUNT; index++) {
        TTHeadTitleView *headCellView = [[TTHeadTitleView alloc]initWithFrame:CGRectMake(self.buttonWidth*index, 0, self.buttonWidth, self.buttonHeight)];
        headCellView.fontColor = self.titleSelectedColor;
        headCellView.date = [self.startMonday dayAfterDays:index];
        headCellView.backgroundColor = self.titleSelectedBakcgroundColor;
        [self.headerSelectedScrollView addSubview:headCellView];
        [self.headCellViewSelectedArray addObject:headCellView];
        
    }
    UIImageView *bottomLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.headerSelectedScrollView.contentSize.height - self.lineHeight, self.headerSelectedScrollView.contentSize.width, self.lineHeight)];
    bottomLine.backgroundColor = self.bottomLineColor;
    [self.headerSelectedScrollView addSubview:bottomLine];
    
}

- (void)btnClick:(UIButton *)button
{
    [self.mainScrollView scrollRectToVisible:CGRectMake(button.tag*self.width, 0, self.mainScrollView.frame.size.width, self.mainScrollView.frame.size.height) animated:YES];
    [self didSelectSegmentIndex:button.tag];
}
- (void)showModaySchedule
{
    [self.mainScrollView scrollRectToVisible:CGRectMake(0*self.width, 0, self.mainScrollView.frame.size.width, self.mainScrollView.frame.size.height) animated:YES];
    [self didSelectSegmentIndex:0];
}

- (void)didSelectSegmentIndex:(NSInteger)index
{
    
}
-(void)reloadEveryDaySchedule{
    for (EveryDayVC *vc in self.everyDayVCArray) {
        [vc reloadTableDate];
    }
}
- (void)correctHeader:(UIScrollView *)scrollView
{
    if (scrollView == _mainScrollView) {
        CGFloat location = _headerSelectedScrollView.contentOffset.x + self.buttonWidth/2 - self.width/2;
        
        if (location <= 0) {
            [UIView animateWithDuration:.3 animations:^{
                _headerScrollView.contentOffset = CGPointMake(0, _headerSelectedScrollView.contentOffset.y);
            }];
        }else if (location >= _headerScrollView.contentSize.width - self.width) {
            [UIView animateWithDuration:.3 animations:^{
                _headerScrollView.contentOffset = CGPointMake(_headerScrollView.contentSize.width - self.width, _headerSelectedScrollView.contentOffset.y);
            }];
        }else {
            if (_headerScrollView.contentOffset.x != location) {
                [UIView animateWithDuration:.3 animations:^{
                    _headerScrollView.contentOffset = CGPointMake(location, _headerSelectedScrollView.contentOffset.y);
                }];
            }
        }
    }
}



#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    ///边缘屏幕滑动1/5滚到上/下一周
    if(scrollView.contentOffset.x > ([self width]*(WEEK_DAY_COUNT-Slide_Size))){
        NSLog(@"nextWeek");
        self.mainScrollView.bounces = NO;
        [self nextWeek];
        return;
    }
    if(scrollView.contentOffset.x < ([self width]*(Slide_Size-1))){
        NSLog(@"preWeek");
        self.mainScrollView.bounces = NO;
        [self preWeek];
        return;
    }
    ///移动日期标签(有中间滑动过程)
    if (scrollView == _mainScrollView) {
        self.headerSelectedAreaShowView.frame = CGRectMake(scrollView.contentOffset.x * (self.buttonWidth/self.width), self.headerSelectedAreaShowView.frame.origin.y, self.headerSelectedAreaShowView.frame.size.width, self.headerSelectedAreaShowView.frame.size.height);
        self.headerSelectedScrollView.contentOffset = CGPointMake(scrollView.contentOffset.x * (self.buttonWidth/self.width), 0);
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    ///确定选中日期标签位置
    if (scrollView == _mainScrollView) {
        [self correctHeader:scrollView];
    }
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    
}




-(void)nextWeek{
    self.startMonday = [self.startMonday dayAfterDays:WEEK_DAY_COUNT];
    
    [self setDateForScheduleWithThisWeekMonday:self.startMonday];
    NSLog(@"第%ld周", [[NSDate getFirstDay] compsWeekToDate:self.startMonday] + 1);
    ///显示周一
    [self showScheduleWithIndex:0];
//    [self performSelector:@selector(openScrollviewBounces) withObject:nil afterDelay:0.8];
    self.mainScrollView.bounces = YES;

}

-(void)preWeek{
    self.startMonday = [self.startMonday dayBeforeDays:WEEK_DAY_COUNT];
    NSLog(@"第%ld周", [[NSDate getFirstDay] compsWeekToDate:self.startMonday] + 1);
    [self setDateForScheduleWithThisWeekMonday:self.startMonday];
    ///显示周日
    [self showScheduleWithIndex:6];
//    [self performSelector:@selector(openScrollviewBounces) withObject:nil afterDelay:0.8];
    self.mainScrollView.bounces = YES;
}

-(void)openScrollviewBounces{
    self.mainScrollView.bounces = YES;
}



#pragma mark - getter
- (CGFloat)height
{
    return self.view.frame.size.height;
}

- (CGFloat)width
{
    return self.view.frame.size.width;
}

- (CGFloat)originX
{
    return self.view.frame.origin.x;
}

- (CGFloat)originY
{
    return self.view.frame.origin.y;
}


- (UIScrollView *)headerScrollView
{
    if(!_headerScrollView) {
        _headerScrollView = [[UIScrollView alloc] init];
        [_headerScrollView setShowsVerticalScrollIndicator:NO];
        [_headerScrollView setShowsHorizontalScrollIndicator:NO];
        _headerScrollView.delegate = self;
        _headerScrollView.bounces = NO;
        _headerScrollView.backgroundColor = self.headerBakcgroundColor;
    }
    return _headerScrollView;
}

- (UIScrollView *)headerSelectedScrollView
{
    if(!_headerSelectedScrollView) {
        _headerSelectedScrollView = [[UIScrollView alloc] init];
        [_headerSelectedScrollView setShowsVerticalScrollIndicator:NO];
        [_headerSelectedScrollView setShowsHorizontalScrollIndicator:NO];
        _headerSelectedScrollView.userInteractionEnabled = NO;
        _headerSelectedScrollView.delegate = self;
        _headerSelectedScrollView.backgroundColor = self.headerBakcgroundColor;
        _headerSelectedScrollView.clipsToBounds = YES;
    }
    return _headerSelectedScrollView;
}

- (CGFloat)buttonHeight
{
    if(_buttonHeight == 0) {
        _buttonHeight = 35 ;
    }
    return _buttonHeight;
}
-(CGFloat)bottomHeight
{
    if(_bottomHeight == 0) {
        _bottomHeight = 0;
    }
    return _bottomHeight;
}

- (CGFloat)buttonWidth
{
    if(_buttonWidth == 0) {
        _buttonWidth = [UIScreen mainScreen].bounds.size.width/WEEK_DAY_COUNT;
    }
    return _buttonWidth;
}

- (CGFloat)lineHeight
{
    if(_lineHeight == 0) {
        _lineHeight = 1;
    }
    return _lineHeight;
}

- (UIColor *)backgroundColor
{
    if(!_backgroundColor) {
        _backgroundColor = [UIColor clearColor];
    }
    return _backgroundColor;
}

- (UIColor *)headerBakcgroundColor
{
    if(!_headerBakcgroundColor) {
        _headerBakcgroundColor = [UIColor whiteColor];
    }
    return _headerBakcgroundColor;
}

- (UIColor *)titleColor
{
    if(!_titleColor) {
        _titleColor = [UIColor blackColor];
    }
    return _titleColor;
}
-(UIColor *)titleBakcgroundColor{
    if(!_titleBakcgroundColor){
        _titleBakcgroundColor = [UIColor lightGrayColor];
    }
    return _titleBakcgroundColor;
}

- (UIColor *)titleSelectedColor
{
    if (!_titleSelectedColor) {
        _titleSelectedColor = [UIColor whiteColor];
    }
    return _titleSelectedColor;
}
-(UIColor *)titleSelectedBakcgroundColor
{
    if(!_titleSelectedBakcgroundColor ){
        _titleSelectedBakcgroundColor = [UIColor whiteColor];
    }
    return _titleSelectedBakcgroundColor;
}
- (UIColor *)bottomLineColor
{
    if(!_bottomLineColor) {
        _bottomLineColor = [UIColor blueColor];
    }
    return _bottomLineColor;
}

@end
